package com.spider.db;

import com.spider.util.StringUtil;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

public class SqlHelper {

    /**
     * 生成 insert 语句
     */
    public static String generateInsertSql(Class<?> entityClass, Map<String,Object> fieldValue)throws NoSuchFieldException {
        StringBuilder sql = new StringBuilder("insert into ").append(StringUtil.camelhumpToUnderline(entityClass.getSimpleName()));
        if (fieldValue != null && fieldValue.size() > 0) {
            int i = 0;
            StringBuilder columns = new StringBuilder(" ");
            StringBuilder values = new StringBuilder(" values ");
            for (String fieldName : fieldValue.keySet()) {
                String columnName = StringUtil.camelhumpToUnderline(fieldName);
                if (i == 0) {
                    columns.append("(").append(columnName);
                    if(fieldValue.get(fieldName) instanceof String || fieldValue.get(fieldName) instanceof Date) {
                        values.append("('").append(fieldValue.get(fieldName)).append("'");
                    }else {
                        values.append("(").append(fieldValue.get(fieldName));
                    }
                } else {
                    columns.append(", ").append(columnName);
                    if(fieldValue.get(fieldName) instanceof String || fieldValue.get(fieldName) instanceof Date) {
                        values.append(", '").append(fieldValue.get(fieldName)).append("'");
                    }else{
                        values.append(", ").append(fieldValue.get(fieldName));
                    }
                }
                if (i == fieldValue.size() - 1) {
                    columns.append(")");
                    values.append(")");
                }
                i++;
            }
            sql.append(columns).append(values);
        }
        return sql.toString();
    }


    /**
     * 生成 delete 语句
     */
    public static String generateDeleteSql(Class<?> entityClass, String condition) {
        StringBuilder sql = new StringBuilder("delete from ").append(entityClass.getSimpleName());
        sql.append(generateWhere(condition));
        return sql.toString();
    }

    /**
     * 生成 update 语句
     */
    public static String generateUpdateSql(Class<?> entityClass, Map<String, Object> fieldMap, String condition)throws NoSuchFieldException {
        StringBuilder sql = new StringBuilder("update ").append(StringUtil.camelhumpToUnderline(entityClass.getSimpleName()));
        if (fieldMap != null && fieldMap.size() > 0) {
            sql.append(" set ");
            int i = 0;
            for (Map.Entry<String, Object> fieldEntry : fieldMap.entrySet()) {
                String fieldName = fieldEntry.getKey();
                String columnName = entityClass.getClass().getField(fieldName).getName();
                if (i == 0) {
                    sql.append(columnName).append(" = ?");
                } else {
                    sql.append(", ").append(columnName).append(" = ?");
                }
                i++;
            }
        }
        sql.append(generateWhere(condition));
        return sql.toString();
    }

    private static String generateWhere(String condition) {
        String where = "";
        if (!StringUtils.isEmpty(condition)) {
            where += " where " + condition;
        }
        return where;
    }





}
